package com.lagou.edu.controller;

import com.lagou.edu.pojo.Resume;
import com.lagou.edu.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/resume")
public class ResumeController {

    @Autowired
    private ResumeService resumeService;

    @RequestMapping(value = "/resumeList",method = RequestMethod.POST)
    public ModelAndView login(HttpServletRequest request, HttpServletResponse response){
        System.out.println("adjfalkdjflkdajflkdjflakdfjlkdasjfladsk");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("resumeList");
        List<Resume> resumeList = resumeService.findResumeList();
        modelAndView.addObject("resumeList",resumeList);
        return modelAndView;
    }

    @RequestMapping(value = "/insertResume",method =  RequestMethod.POST)
    public void insertResume(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String address = request.getParameter("address");
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        int i = resumeService.insertResume(address, name, phone);
        response.sendRedirect("/resume/resumeList");
    }

    @RequestMapping(value = "/updateResume",method =  RequestMethod.POST)
    public void updateResume(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        String address = request.getParameter("address");
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        resumeService.updateResume(Long.parseLong(id),address,name,phone);
        response.sendRedirect("/resume/resumeList");
    }

    @RequestMapping(value = "/deleteResume",method =  RequestMethod.GET)
    public void deleteResume(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        resumeService.delResume(Long.parseLong(id));
        response.sendRedirect("/resume/resumeList");
    }

    @RequestMapping(value = "/updateResumePage",method =  RequestMethod.GET)
    public ModelAndView updateResumePage(HttpServletRequest request, HttpServletResponse response){
        ModelAndView modelAndView = new ModelAndView();
        String id = request.getParameter("id");
        Resume one = resumeService.findOne(Long.parseLong(id));
        modelAndView.addObject("resume",one);
        modelAndView.setViewName("updateResumePage");
        return modelAndView;
    }

    @RequestMapping(value = "/addResume",method =  RequestMethod.GET)
    public ModelAndView addResume(HttpServletRequest request, HttpServletResponse response){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("addResume");
        return modelAndView;
    }

}
